//
//  BQShopCollectionViewCell.m
//  BeeQuick
//
//  Created by Asun on 2017/7/10.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQShopCollectionViewCell.h"
#import "BQShopModelArrayManager.h"
@interface BQShopCollectionViewCell ()

@property (weak, nonatomic) IBOutlet UIImageView *iconImgView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@property (weak, nonatomic) IBOutlet UIImageView *jingxuanImageView;
@property (weak, nonatomic) IBOutlet UIImageView *buyOneImageView;


@property (weak, nonatomic) IBOutlet UILabel *specificsLabel;

@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *market_priceLabel;


@property (weak, nonatomic) IBOutlet UIButton *decreaseBtn;

@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@property (weak, nonatomic) IBOutlet UIButton *increaseBtn;




@end

@implementation BQShopCollectionViewCell

- (void)awakeFromNib{
    [super awakeFromNib];
    
    [self addObserver:self forKeyPath:@"model.count" options:NSKeyValueObservingOptionNew context:nil];
}


- (void)setModel:(BQProductModel *)model{
    
    _model = model;  
    
    [self.iconImgView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:[UIImage imageNamed:@"v2_placeholder_half_size"]];
    
    self.nameLabel.text = model.name;
    
    self.specificsLabel.text = model.specifics;
    
    self.priceLabel.text = [NSString stringWithFormat:@"$%.2f",model.price];
    
    self.market_priceLabel.text = [NSString stringWithFormat:@"$%.2f",model.market_price];
    
    self.buyOneImageView.hidden = !model.had_pm;
    
    self.decreaseBtn.hidden = YES;
    
    self.countLabel.hidden = YES;
}

- (IBAction)increase:(UIButton *)sender {
    
    if (sender == _decreaseBtn) {
        
        [[BQShopModelArrayManager manager] shopModelArrayManagerReduceWithModel:self.model];
        
        self.model.count--;
        
    } else {
        
        if (self.model.count >= self.model.number) {
            
            NSString * str = [self.model.name stringByAppendingString:@"库存不足了,请等待货源充足再购买~"];
            
            [SVProgressHUD showImage:[UIImage imageNamed:@"v2_orderSuccess"] status:str];
            
            [SVProgressHUD dismissWithDelay:1.0];
            
            return;
        }
        
        [[BQShopModelArrayManager manager] shopModelArrayManagerAddWithModel:self.model];
        
        self.model.count++;
        
        CGPoint center = self.iconImgView.center;
        
        CGPoint targetPoint = [self.iconImgView convertPoint:center toView:kWindow];
        
        NSValue *targetValue = [NSValue valueWithCGPoint:targetPoint];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:ToValueNotificationName object:self.model userInfo:@{@"point":targetValue,@"num":@(_num)}];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:ToClickButtonNotificationName object:self.increaseBtn];
    }
    
    [[BQShopModelArrayManager manager] tabBarShowCount];
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"model.count"]) {
        
        _decreaseBtn.hidden = !self.model.count;
        
        _countLabel.hidden = !self.model.count;
        
        self.countLabel.text = [NSString stringWithFormat:@"%zd", self.model.count];
        
    }
}

-(void)dealloc {
    
    [self removeObserver:self forKeyPath:@"model.count"];
    
}


@end
